#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: resvisor.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: a resource view

import gtk

from vazaarlib.namespaces import NIE
from vazaarlib.utils import scale2pixbuf
from vazaarlib.constants import IMAGES_DIR

class Visor(gtk.VBox):
    def __init__(self, gui, id, widget):
        gtk.VBox.__init__(self)
        self.gui = gui
        self.id = id
        #print "visor",
        self.vres = self.gui.app.vstore.get_virtual_resource(id)
        #self.properties = self.gui.ask.get_properties(id)
        #self.__create_toolbar()
        self.__create_visor(widget)
        #self.pack_start(self.toolbar, False, True)
        self.pack_end(self.visor, True, True)
        self.show_all()

    def __create_visor(self, widget):
        self.visor = gtk.ScrolledWindow()
        self.visor.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.visor.set_shadow_type(gtk.SHADOW_NONE)
        if not widget:
            widget = gtk.Label("En pruebas")
        view = gtk.Viewport()
        view.set_shadow_type(gtk.SHADOW_NONE)
        view.add(widget)
        self.visor.add(view)
        self.visor.show_all()

    def __create_button(self, icon, function, name):
        vbox = gtk.VBox()
        button = gtk.ToolButton()
        filename = IMAGES_DIR + "/" + icon
        pixbuf = scale2pixbuf(24, 24, filename)
        image = gtk.image_new_from_pixbuf(pixbuf)
        label = gtk.Label(name)
        vbox.pack_start(image)
        button.set_icon_widget(vbox)
        button.connect('clicked', function, self.id)
        child = button.get_children()[0]
        child.set_focus_on_click(False)

        return button

    def __create_toolbar(self):
        self.toolbar = gtk.VBox() # Toolbar container
        header = gtk.HBox() # icon & resource name
        buttons = gtk.Toolbar() # buttons
        buttons.set_style(gtk.TOOLBAR_ICONS)
        buttons.set_icon_size(gtk.ICON_SIZE_MENU)

        # Build header
        """
        icon = gtk.image_new_from_stock(gtk.STOCK_YES, gtk.ICON_SIZE_DND)
        label = gtk.Label("<big>Resource name</big>")
        label.set_use_markup(True)
        label.set_property('justify', gtk.JUSTIFY_LEFT)
        header.pack_start(icon, False, True)
        header.pack_end(label)
        """
        # Build toolbar
        button = self.__create_button("tag-icon.png", self.gui.actions.tag_resource, "Tag")
        buttons.insert(button, -1)
        button = self.__create_button("edit-icon.png", self.gui.actions.annotate_resource, "Edit")
        buttons.insert(button, -1)

        button = gtk.ToolItem()
        vbtitle = gtk.VBox()
        title = gtk.Button()
        title.set_label(str(self.vres.prop[NIE['title']][0]))
        title.set_relief(gtk.RELIEF_NONE)
        title.set_use_underline(False)
        title.connect('clicked', self.gui.actions.show_edit_resource_dialog)
        #lentitle = len(str(self.vres.prop[NIE['title']][0]))
        #if lentitle < 50:
        #    title.set_width_chars(lentitle)
        #title.set_width_chars(75)

        vbtitle.pack_end(title, True, True)
        button.add(vbtitle)
        buttons.insert(button, -1)

        """
        button = gtk.ToolItem()
        nothing = gtk.EventBox()
        box = gtk.VBox()
        box.pack_end(nothing, True, True)
        button.add(box)
        buttons.insert(button, -1)
        """

        # Pack header and toolbar
        #header.show_all()
        buttons.show_all()
        #self.toolbar.pack_start(header, False, True)
        self.toolbar.pack_end(buttons, True, True)
        self.toolbar.show_all()

    def do_nothing(self, *args):
        print "tagging resource %s" % self.id

if __name__ == "__main__":
    v = Visor(None)
    w = gtk.Window()
    w.add(v)
    w.connect('delete-event', gtk.main_quit)
    w.show_all()
    gtk.main()

"""
        content = gtk.ScrolledWindow()
        content.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        if self.widget:
            content.add(self.widget)
        content.show_all()
"""
